auto.waitFor();
//console.show();

var myFunc = require("myCommonFunction.js");

renwu();

function opreate() {
    var ret = myFunc.initNetWork(null, true);
    threads.start(function () {
        if (ret) {
            pingduoduo();
        }
        myFunc.tuichu("拼多多");
        exit();
    });

    threads.start(function () {
        sleep(60000)
        exit();
    });


}

function pingduoduo() {
    myFunc.openMyApp("拼多多");

    log("点击个人中心");
    var geren = className("android.widget.TextView").text("个人中心").findOne(5000);
    if (geren == null) {
        log("没找到 个人中心");
        myFunc.tuichu("拼多多");
        sleep(500);
        var ret = myFunc.initNetWork(null, useWiFi);
        if (ret) {
            pingduoduo();
        }
        return;
    } else {
        myFunc.pClick(geren);
    }
    log("点击月卡");
    sleep(1000);
    var yueka = text("省钱月卡").findOne(5000);
    if (yueka == null) {
        log("没找到 省钱月卡");
        myFunc.tuichu("拼多多");
        sleep(500);
        pingduoduo();
        return;
    } else {
        myFunc.pClick(yueka);
    }
    sleep(1000);
    log("点击领券");
    var shiyong = text("去领无门槛券").findOne(5000);
    log("点击领券:" + (shiyong != null));
    if (shiyong == null) {
        log("没找到 点击领券");
        myFunc.tuichu("拼多多");
        sleep(500);
        pingduoduo();
        return;
    } else {
        myFunc.clickCenter(shiyong);
        renwu();
    }
}

function renwu(){
    sleep(2000);
    click(1240, 2911);
    sleep(1000);
    if(text("每日体力任务").exists()) {
        var a = textContains("浏览趣味活动").findOnce();
        if(a != null) {
            a = getQuWanCheng(a);
            if(a != null) {
                myFunc.pClick(a);
                sleep(2000);
                click(711,1260);
                for (let index = 0; index < 60; index++) {
                    sleep(1500);
                    swipe(500, 1200, 500, 600, 500)
                    sleep(500);
                }
                back();
                textContains("收下").findOne(3000).click();
                renwu();
            }
        }
        a = textContains("30秒").findOnce();
        if(a != null) {
            a = getQuWanCheng(a);
            if(a != null) {
                myFunc.pClick(a);
                for (let index = 0; index < 30; index++) {
                    sleep(1000);
                    swipe(500, 1200, 500, 600, 500)
                    sleep(500);
                }
                textContains("收下").findOne(3000).click();
            }
        }
    } else{
        for (let index = 0; index <3; index++) {
            back();
            var shiyong = className("android.view.View").text("去领无门槛券").findOne(2000);
            if(shiyong!= null) {
                myFunc.clickCenter(shiyong);
                renwu();
            }
            sleep(1000);
        }
    }
}

function getQuWanCheng(a) {
    var b = a.bounds();
    var c = 80;
    var ret = text("去完成").boundsInside(1000, b.top - c, 1440, b.bottom + c).findOnce();
    myFunc.log(a.text(), "去完成==null", ret == null);
    return ret;
}

                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                            